package kr.saeildamil.androidstudy_second.fragment.common;

import kr.saeildamil.andrordstudy.second.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class CounterFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		/*방법 1		
		View root = inflater.inflate(R.layout.ch20_counterfragment, container);
		final TextView textCounter = (TextView) root.findViewById(R.id.counterfragment_txtcounter);
		
		root.findViewById(R.id.counterfragment_btn).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				int count = Integer.parseInt(textCounter.getText().toString());
				textCounter.setText(Integer.toString(count+1));
			}
		});*/
		
		/* 방법 2 
		LinearLayout root = new LinearLayout(getActivity());
		root.setOrientation(LinearLayout.VERTICAL);
		root.setBackgroundColor(Color.YELLOW);
		root.setGravity(Gravity.CENTER_HORIZONTAL);
		
		final TextView textCounter = new TextView(getActivity());
		textCounter.setText("0");
		textCounter.setTextColor(Color.RED);
		textCounter.setTextSize(40);
		
		Button btnIncrease = new Button(getActivity());
		btnIncrease.setText("Increase");
		
		LinearLayout.LayoutParams param = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		
		root.addView(textCounter, param);
		root.addView(btnIncrease, param);
		
		btnIncrease.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				int count = Integer.parseInt(textCounter.getText().toString());
				textCounter.setText(Integer.toString(count+1));
			}
		});
		*/
		
		/*방법 3
		View root = inflater.inflate(R.layout.ch20_counterfragment, container);
		Button btnIncrease = (Button) getActivity().findViewById(R.id.counterfragment_btn);
		final TextView textCounter = (TextView) getActivity().findViewById(R.id.counterfragment_txtcounter);
		
		btnIncrease.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				int count = Integer.parseInt(textCounter.getText().toString());
				textCounter.setText(Integer.toString(count+1));
			}
		});
		*/
		/* 방법 4 onActivityCreate에서 구현 */
		View root = inflater.inflate(R.layout.ch20_counterfragment, container);
		
		return root;
	}//end of method onCreateView
	
	/* 방법 4 onActivityCreate에서 구현 getActivity() */
	/* 방법 5 onActivityCreate에서 구현  getView() */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Button btnIncrease = (Button)getView().findViewById(R.id.counterfragment_btn);
		final TextView textCounter = (TextView) getView().findViewById(R.id.counterfragment_txtcounter);
		
		btnIncrease.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				int count = Integer.parseInt(textCounter.getText().toString());
				textCounter.setText(Integer.toString(count+1));
			}
		});
	}
}








